﻿using Autofac;
using Autofac.Extensions.DependencyInjection;
using Autofac.Extras.Quartz;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SimulationCenter.Configs;
using SimulationCenter.Data;
using SimulationCenter.Jobs;
using SimulationCenter.Schedulers;
using SimulationCenter.Scripts;
using SimulationCenter.Servers;
using SimulationCenter.Tasks.Svrs;
using Swashbuckle.AspNetCore.Swagger;
using System;

namespace SimulationCenter
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                options.CheckConsentNeeded = context => false;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            var adbCenterOptions = new AdbCenterOptions();
            Configuration.Bind("AdbCenterOptions", adbCenterOptions);
            services.AddSingleton(adbCenterOptions);

            services.AddTransient<SqlProvider>();
            services.AddSingleton<AdbCenter>();
            services.AddSingleton<ScriptManager>();
            services.AddSingleton<ScScheduleManager>();
            services.AddTransient<ITaskSvr, TaskSvr>();
            services.AddTransient<ISubscriptionSvr, SubscriptionSvr>();
            services.AddSingleton<ServerManager>();
            services.AddLogging();

            services.AddCors(s =>
                s.AddPolicy("corsPolicy",
                    c => c.AllowAnyOrigin()
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    .AllowCredentials()));

            services.AddSignalR();

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info { Title = "API", Version = "v1" });

                c.IncludeXmlComments("SimulationCenter.xml");
            });

            var iocBuilder = new ContainerBuilder();
            iocBuilder.Populate(services);

            iocBuilder.RegisterModule(new QuartzAutofacFactoryModule());
            iocBuilder.RegisterModule(new QuartzAutofacJobsModule(typeof(ScStartUpJob).Assembly));

            var ioc = iocBuilder.Build();
            return new AutofacServiceProvider(ioc);
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseCors("corsPolicy");
            app.UseSignalR(routes => routes.MapHub<AdbHub>("/adb"));
            app.UseSwagger();
            app.UseSwaggerUI(setup => setup.SwaggerEndpoint("/swagger/v1/swagger.json", "API V1"));
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
